package com.tmc.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tmc.bean.common.CommonResult;
import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.common.Pagination;
import com.tmc.entity.common.EthUser;
import com.tmc.service.baseinfo.EthUserServiceImpl;
import com.tmc.web.BaseController;

@Controller
@RequestMapping(value="/user")
public class EthUserController extends BaseController{
	
	@Autowired
	private EthUserServiceImpl userService;
	
	@RequestMapping(value="/list", method = RequestMethod.GET)
	public ModelAndView findList(EthUser cond, Integer pageIndex, Integer pageSize, HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		if (pageIndex == null || pageIndex < 1)
			pageIndex = 1;
		if (pageSize == null || pageIndex < 1 || pageIndex > 1000)
			pageSize = 10;
		
		Map<String, Object> map = new HashMap<String, Object>();
		Pagination<EthUser> page = userService.findByCond(context, cond, pageIndex,pageSize);
		map.put("cond", cond);
		map.put("page", page);
		map.put("context", context);
		return new ModelAndView("views/user/user-list", map);
	}
	
	@RequestMapping(value="/detail", method = RequestMethod.GET)
	public ModelAndView edit(HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		EthUser user = userService.getUserById(context,context.getUserId());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user", user);
		map.put("context", context);
		return new ModelAndView("views/user/user-detail", map);
	}
	
	@RequestMapping(value="/updatePasswordEdit",method = RequestMethod.GET)
	public ModelAndView updatePasswordEdit(Long userId, HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		EthUser user = userService.getUserById(context,userId);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user", user);
		map.put("context", context);
		return new ModelAndView("views/user/user-update-password", map);
	}
	
	
	@RequestMapping(value="/updatePhone", method = RequestMethod.POST)
	@ResponseBody
	public String updatePhone(EthUser user, HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		CommonResult cr = userService.updatePhone(context, user);
		return cr.toJson();
	}
	
	@RequestMapping(value="/updatePassword", method = RequestMethod.POST)
	@ResponseBody
	public String updatePassword(EthUser user, HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		CommonResult cr = userService.updatePassword(context, user);
		return cr.toJson();
	}
	
	
	@RequestMapping(value="/save",method = RequestMethod.POST)
	@ResponseBody
	public String save(EthUser user, HttpServletRequest request){
		CommonResult cr = userService.save(new ContextInfo(), user);
		return cr.toJson();
	}
	
	@RequestMapping(value="/delete",method = RequestMethod.GET)
	@ResponseBody
	public String delete(Long userId, HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		CommonResult cr = userService.delete(context, userId);
		return cr.toJson();
	}
	
	@RequestMapping(value="/editPage",method = RequestMethod.GET)
	public ModelAndView editPage(Long userId, HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		EthUser user = null;
		if(userId != null){
			user = userService.getUserById(context,userId);
		}else{
			user = new EthUser();
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user", user);
		map.put("context", context);
		return new ModelAndView("views/user/user-edit", map);
	}
	
	@RequestMapping(value="/search", method=RequestMethod.POST)
	@ResponseBody
	public String search(String query){
		List<EthUser> list=null;
		list = userService.searchOperator(query);
		JSONArray array = new JSONArray();
		for(EthUser user : list){
			//"Key":"clientId","Name":"userName" 注意，键和值都是字符串
			JSONObject obj = new JSONObject();
			obj.put("Key", user.getUserId() 
					+ "");
			obj.put("Name",user.getUserName()+"("+user.getUserCode()+")");
			array.add(obj);
		}
 		return array.toJSONString();	
	}
	
}
